<?php
	class common extends Mysql{
		
		public $pages_path = 'http://115.28.175.114/tdt_api/pages/';
		public $pages_intro = 'goods_intro.php?';
		
		function __construct(){
			parent::__construct();
			$this->pages_intro = $this->pages_path.$this->pages_intro;
		}
		
		/**
		 * 保存提示
		 * @param $msg
		 * @param $url
		 */
		function submitAlert($msg,$url){
			$alert = "<script>
						alert('".$msg."');
						window.location.href='".$url."';
					</script>";
			echo $alert;
		}
		
		/**
		 * 获得省市区
		 * @param $type
		 * @param $father
		 * @param $selected
		 */
		function getArea($type,$father='',$selected=''){
			$sql="pcc_".$type." where fatherid='".$father."'";
			$result = $this->getList($sql);
			$child = $result['result'];
			if(is_array($child)){
				foreach($child as $k=>$v){
					if($selected==$v[$type.'id']) $child[$k]['selected']=' selected="selected"';
				}
			}
			return $child;
		}
		
		/**
		 * 获得数量
		 * @param $table
		 * @param $where
		 * @param $left
		 * @param $group
		 */
		function getNum($sql,$key="",$group=false){
			//是否有group by
			if(strpos($sql, "group by")){
				$group = true;
			}
			if($key!='' && $key!="*"){
				$key = "count(*) as num,".$key;
			}else{
				$key = "count(*) as num";
			}
			$sql="select ".$key." from ".$sql;
			if($group==true) $sql = "select count(*) as num from (".$sql.") as test";
			$query=$this->query($sql);
			$row=$this->fetch_array($query);
			$num=$row['num'];
			return $num;
		}
		function my_array_unique($from) {
			for ($i=count($from)-1;$i>1;$i--) {
				$last = $from[$i];
				$from[$i] = false;
				if (!in_array($last,$from)) {
					$from[$i]=$last;   
				}
			}
			$from = array_unique($from);
			$from = array_slice($from,0,count($from)-1);
			return $from;
        } 
		
		/**
		 * 获得列表
		 * @param $table
		 * @param $key
		 * @param $where
		 * @param $left
		 * @param $page
		 */
		function getList($sql, $key="*", $num='0', $page='1' ,$debug='0'){
			$sql_num = $sql;
			$sql = "select ".$key." from ".$sql;
			if($debug=='1') echo $sql."<br/>";
			if($num!='0' && $page>=1){
				$totals = $this->getNum($sql_num,$key);
				$total_page = ceil($totals/$num);
				$query = $this->query($sql." limit ".($page-1)*$num.",".$num);
			}else{
				$query = $this->query($sql);
			}
			while($row = $this->fetch_array($query)){
				$result[]=$row;
			}
			
			$return['result'] = $result;
			$return['total_num'] = $totals;
			$return['total_page'] = $total_page;
			return $return;
		}
		
		/**
		 * 获得内容
		 * @param $table
		 * @param $where
		 * @param $key
		 */
		function getInfo($sql, $key='*',$debug='0'){
			$sql="select ".$key." from ".$sql;
			if($debug=='1') echo $sql."<br/>";
			$query=$this->query($sql);
			$result=$this->fetch_array($query);
			return $result;
		}
		
		/**
		 * 将时间戳转换成网站时间
		 * @param unknown_type $date
		 * @return string
		 */
		function getDate($date){
			$now=time();
			$today=date("m月d日");
			$timelag=$now-$date;
			if($timelag<=60){
				$time=$timelag."秒前";
			}elseif($timelag<=3600){
				$time=floor($timelag/60)."分钟前";
			}else{
				$day=date("m月d日",$date);
				$time=date("H:i",$date);
				if($day==$today){
					$time="今天&nbsp;".$time;
				}else{
					if(date("Y",$date)!=date("Y")){
						$time=date("Y年").$day.$time;
					}else{
						$time=$day.$time;
					}
				}
			}
			return $time;
		}
		
		/**
		 * 删除
		 * @param $table
		 * @param $id
		 * @param $img
		 * @param $dir
		 */
//		function del($table, $id, $file='', $relation='', $search_key="id"){
//			if($id!=""){
//				$id=substr($id,0,-1);
//				
//				if(is_array($file)){
//					foreach($file as $k=>$v){
//						$key = $v['key'];
//						$sql = $table." where ".$search_key." in (".$id.")";
//						$keys = $this->getList($sql,'',$key);
//						$keys = $keys['result'];
//						if(is_array($keys)){
//							foreach($keys as $key=>$val){
//								@unlink($v['dir'].$val[$v['key']]);
//							}
//						}
//					}
//				}
//				
//				if(is_array($relation)){
//					foreach($relation as $k=>$v){
//						//删除关联数据
//						$sql = "delete from `".$v['table']."` where `".$v['key']."` in (".$id.")";
//						$this->query($sql);
//					}
//				}
//				
//				$sql="delete from `".$table."` where ".$search_key." in (".$id.")";
//				$result = $this->query($sql);
//			}else{
//				$result = '0';
//			}
//			return $result;
//		}
		// 浏览器友好的变量输出
		function dump($var, $echo=true, $label=null, $strict=true) {
			$label = ($label === null) ? '' : rtrim($label) . ' ';
			if (!$strict) {
				if (ini_get('html_errors')) {
					$output = print_r($var, true);
					$output = "<pre>" . $label . htmlspecialchars($output, ENT_QUOTES) . "</pre>";
				} else {
					$output = $label . print_r($var, true);
				}
			} else {
				ob_start();
				var_dump($var);
				$output = ob_get_clean();
				if (!extension_loaded('xdebug')) {
					$output = preg_replace("/\]\=\>\n(\s+)/m", "] => ", $output);
					$output = '<pre>' . $label . htmlspecialchars($output, ENT_QUOTES) . '</pre>';
				}
			}
			if ($echo) {
				echo($output);
				return null;
			}else
				return $output;
		}
		
		/**
		 * 二维数组转换成字符串
		 * @author	jf	
		 * @param 	array 	$arr
		 * @return 	string
		 */
		function convert($arr){
			if(!empty($arr)){
				foreach ($arr as $v){
					$v = join(",",$v);
					$temp[] = $v;
				}
				$t="";
				foreach($temp as $v){
					$t.=$v.",";
				}
				$t=substr($t,0,-1);
			}
			return $t;
		}
		
		/**
		 * 二维数组转换成字符串3.0版本
		 * @author --wxy
		 * @param 	array 	$arr
		 * @param	string	$key
		 * @return 	string
		 */
		function convert2($arr,$key='id'){
			$t="";
			if(!empty($arr)){
				foreach($arr as $v){
					if( $v[$key]!='')
						$temp[]= $v[$key];
				}
				if(is_array($temp)){
					$temp = array_unique($temp);
					$t = join(",",$temp);
				}
			}
			return $t;
		}
		
		/**
		 * 加密算法
		 * @param $data
		 * @param $key
		 */
		function encrypt($data, $key='runer'){
			$key = md5($key);
			$x = 0;
			$len = strlen($data);
			$l = strlen($key);
			for($i = 0; $i < $len; $i++){
				if ($x == $l){
					$x = 0;
				}
				$char .= $key{$x};
				$x++;
			}
			for($i = 0; $i < $len; $i++){
				$str .= chr(ord($data{$i}) + (ord($char{$i})) % 256);
			}
			return str_replace("+","%2B",base64_encode($str));
		}
		
		/**
		 * 解密算法
		 * @param $data
		 * @param $key
		 */
		function decrypt($data, $key='runer'){
			$key = md5($key);
			$x = 0;
			$data = base64_decode(str_replace("%2B", "+", $data));
			$len = strlen($data);
			$l = strlen($key);
			for($i = 0; $i < $len; $i++){
				if ($x == $l){
					$x = 0;
				}
				$char .= substr($key, $x, 1);
				$x++;
			}
			for($i = 0; $i < $len; $i++){
				if (ord(substr($data, $i, 1)) < ord(substr($char, $i, 1))){
					$str .= chr((ord(substr($data, $i, 1)) + 256) - ord(substr($char, $i, 1)));
				}else{
					$str .= chr(ord(substr($data, $i, 1)) - ord(substr($char, $i, 1)));
				}
			}
			return $str;
		}
		
		/**
		 * 重组标题
		 * @param    array		$arr
		 * @param	 array		$attrs
		 * @return 	 string
		 */
		function title($param){
			extract($param);
			for($j=0;$j<count($attrs);$j++){
				if($arr['attr1_id'] == $attrs[$j]['id']){
					$arr['title'] = $arr['title']." ".$attrs[$j]['value']." ";
				}
				if($arr['attr2_id'] == $attrs[$j]['id']){
					$arr['title'] = $arr['title']." ".$attrs[$j]['value']." ";
				}
				if($arr['attr3_id'] == $attrs[$j]['id']){
					$arr['title'] = $arr['title']." ".$attrs[$j]['value']." ";
				}
			}
			return $arr['title'];
		}
		
		/**
		 * 获取多表单值-jf
		 * @param array  $arr   表单名称
		 * @param string $type 	P为post  G为get
		 * @return array
		 */
		function getFrom($arr,$type){
			if(!empty($arr)){
				if($type === 'P'){
					$a = $_POST;
				}else if($type === 'G'){
					$a = $_GET;
				}
				for($i=0;$i<count($arr);$i++){
					$res[$arr[$i]] = $a[$arr[$i]];
				}
			}
			return $res;
		}
		
		/**
		 * url 重组
		 * @param unknown $key
		 * @param unknown $value
		 * @return string
		 */
	 	function join_url($key,$value){
	 		$num = 0;
	 		parse_str($_SERVER["QUERY_STRING"],$url_arr);
	 		foreach ( $url_arr  as $k => $v){
	 			if($k == $key){
	 				$row[$k] = $v.",".$value;
	 				$num = 1;
	 			}else{
	 				$row[$k] = $v;
	 			}
	 		}
	 		if($num == 0){
	 			$row[$key] =$value;
	 		}
	 		return http_build_query($row);
		} 
		
		/**
		 * 替换URL
		 */
		function replace_url($key,$value){
			$num = 0;
			parse_str($_SERVER["QUERY_STRING"],$url_arr);
			foreach ( $url_arr  as $k => $v){
				if($k == $key){
					$row[$k] = $value;
					$num = 1;
				}else{
					$row[$k] = $v;
				}
			}
			if($num == 0){
				$row[$key] =$value;
			}
			return http_build_query($row);
		}
		
		// 获取文章中的图片
		function getImgs($content,$order='ALL'){
   			$pattern="/<img.*?src=[\'|\"](.*?(?:[\.gif|\.jpg]))[\'|\"].*?[\/]?>/";
    		preg_match_all($pattern,$content,$match);
    		if(isset($match[1])&&!empty($match[1])){
       			if($order==='ALL'){
            		return $match[1];
        		}
        		if(is_numeric($order)&&isset($match[1][$order])){
            		return $match[1][$order];
        		}
    		}
    		return '';
		}

		/**
		 * 手机号码隐藏中间4位-jsy
		 * @param string $phone    手机号码
		 * @return string
		 */
		function hidtel($phone){
	   		 $IsWhat = preg_match('/(0[0-9]{2,3}[-]?[2-9][0-9]{6,7}[-]?[0-9]?)/i',$phone); //固定电话
	    if($IsWhat == 1){
	        return preg_replace('/(0[0-9]{2,3}[-]?[2-9])[0-9]{3,4}([0-9]{3}[-]?[0-9]?)/i','$1****$2',$phone);
	    }else{
	        return  preg_replace('/(1[358]{1}[0-9])[0-9]{4}([0-9]{4})/i','$1****$2',$phone);
	    }
}


		
	}
?>